/*
 * Copyright (C) 2004, Velocityme Partnership
 * StatusActions.java
 *
 * Created on 09 October 2004, 02:47
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.interfaces.*;
import com.velocityme.valueobjects.*;
import com.velocityme.www.actionforms.nodeactionforms.StatusValueActionForm;
import javax.servlet.http.*;
import org.apache.struts.action.*;

/**
 *
 * @author  Robert
 */
public class StatusActions extends NodeActions {
    
    /** Creates a new instance of StatusActions */
    public StatusActions() {
    }
    
    public ActionForward show(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        StatusPK statusPK = new StatusPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("statusValue", cs.getStatusValue(key, statusPK));
        return mapping.findForward("ACTION_DISPLAY");
    }
    
    public ActionForward showCreate(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        StatusPK statusPK = new StatusPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("parentNodePK", nodePK);
        StatusValueActionForm statusValueActionForm = new StatusValueActionForm();
        session.setAttribute("statusActionForm", statusValueActionForm);
        StatusValue statusValue = new StatusValue();
        statusValue.setNodeValue(new NodeValue());
        session.setAttribute("statusValue", statusValue);
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward showEdit(ActionMapping mapping, 
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        StatusPK statusPK = new StatusPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        StatusValue statusValue = cs.getStatusValue(key, statusPK);
        session.setAttribute("statusValue", statusValue);
        StatusValueActionForm statusValueActionForm = new StatusValueActionForm();
        statusValueActionForm.setName(statusValue.getNodeValue().getName());
        statusValueActionForm.setDescription(statusValue.getNodeValue().getDescription());
        session.setAttribute("statusActionForm", statusValueActionForm);
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward edit(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        NodePK nodePK;
        if (request.getParameter("btnSubmit").equals("OK")) {
            StatusValue statusValue = (StatusValue)session.getAttribute("statusValue");
            statusValue.getNodeValue().setName(((StatusValueActionForm)form).getName());
            statusValue.getNodeValue().setDescription(((StatusValueActionForm)form).getDescription());
            StatusPK statusPK;
            if (statusValue.statusIdHasBeenSet()) {
                statusPK = statusValue.getPrimaryKey();
                cs.statusEdit(key, statusPK, statusValue);
            }
            else {
                NodePK parentNodePK = (NodePK)session.getAttribute("parentNodePK");
                statusPK = cs.statusCreate(key, parentNodePK, statusValue);
            }
            nodePK = new NodePK(statusPK.getStatusId());
        }
        // Cancel simply displays the original node again
        else {
            nodePK = new NodePK((Integer)session.getAttribute("nodeId"));
        }
        request.setAttribute("nodePK", nodePK);
        return mapping.findForward("ACTION_SUCCESS");
    }
    
}
